Skip to content

🐛 fix(mcp): bound startup fan-out to stop connect timeouts - #11

Merged
SWSAmor merged 1 commit into
mainfrom
fix/mcp-startup-concurrency-retry
Jul 24, 2026
Merged

SWSAmor merged 1 commit into
mainfrom
fix/mcp-startup-concurrency-retry

Conversation

@SWSAmor

@SWSAmor SWSAmor commented Jul 19, 2026

Copy link
Copy Markdown
Owner

Problem

Downstream MCP servers (most visibly basic-memory) intermittently failed to
appear via code-executor, while other servers in the same session were fine.
Root cause confirmed from mcp-server-code-executor.log:

✗ basic-memory (15006ms): connect timed out after 15000ms
📋 MCP server startup report (14/15 connected)

basic-memory was the slowest server and sat right at the connect timeout,
oscillating between ~3s and 15s+ across restarts.

Root cause

All downstream servers were connected under one unbounded Promise.all.
With ~15 servers, many cold-starting uvx/npx children contend for CPU/IO
(a thundering herd) and starve each other's connect handshake — the log even
shows a run where all 15 timed out at once (~15s). The slowest server
crossed the 15s deadline, was dropped from the pool, and — with no retry and no
lazy reconnect — stayed missing until the host respawned the process.

Fix

  • Bounded startup fan-out — replace the unbounded Promise.all with a new
    mapWithConcurrency helper (sliding window). At most
    POOL_STARTUP_CONCURRENCY (default 6) servers spawn+handshake at once,
    removing the herd. The helper preserves input order and falls back to serial
    on a bad limit (never drops items).
  • Serial retry — servers that miss the first window are retried one at a
    time (POOL_STARTUP_RETRIES, default 1) after the herd clears, so a
    slow server gets the machine to itself instead of being dropped for the
    process lifetime. Retry-recovered servers are marked [via retry] in the
    startup report.
  • Timeout headroom — POOL_CONNECT_TIMEOUT_MS default 15s → 20s.

New config knobs

Env var Default Bounds Purpose
POOL_STARTUP_CONCURRENCY 6 1–64 Max servers connecting at once at startup
POOL_STARTUP_RETRIES 1 0–5 Serial connect retries after the first window
POOL_CONNECT_TIMEOUT_MS 20000 1000–120000 Per-server connect timeout (was 15000)

Verification

  • Unit tests: tests/map-with-concurrency.test.ts (order preservation,
    full coverage, concurrency-cap enforcement, empty/oversized/<1 limit) +
    startup-config validation tests. Two stale getPoolConfig mocks updated to
    the new schema.
  • No regression: the client-pool test area is green (121 tests); the only
    suite-wide failures are pre-existing filesystem/systemd tests that also fail
    on the unmodified baseline.
  • Live (15-server config, host restarted onto the rebuilt binary):
    basic-memory ✓ 3189ms, 15/15 connected, and the whole pool comes up
    ~50–70% faster because the herd is gone (e.g. Context7 4.2s→1.6s,
    desktop-commander 5.0s→2.9s, duckdb 3.1s→1.2s — servers whose own config was
    untouched, so the speedup is purely the bounded fan-out).

Note: the deployed artifact is a bun standalone binary; rebuilt with
npm run build:binary and smoke-tested (closed stdin → clean exit 0).

🤖 Generated with Claude Code

Downstream MCP servers were all connected under one unbounded Promise.all.
With ~15 servers, many cold-starting uvx/npx children contended for CPU/IO
(a "thundering herd"), pushing the slowest (basic-memory) past the 15s
connect timeout. It was then silently dropped from the pool while faster
servers stayed available in the same session, with no retry until the host
respawned the process.

- Bound the startup fan-out to POOL_STARTUP_CONCURRENCY (default 6) via a
  new mapWithConcurrency helper, so cold-starting children no longer starve
  each other's connect handshake.
- Retry servers that miss the first window serially (POOL_STARTUP_RETRIES,
  default 1) once the herd has cleared, so a slow server connects on the
  retry instead of being dropped for the whole process lifetime.
- Raise POOL_CONNECT_TIMEOUT_MS default 15s -> 20s for extra headroom.
- Report retry-recovered servers with a [via retry] marker.

Measured on a 15-server config: the whole pool comes up ~50-70% faster
(herd gone) and basic-memory connects in ~3s instead of timing out at 15s
(15/15 vs the previous 14/15).

Tests: mapWithConcurrency unit tests (order, coverage, concurrency cap,
edge cases) and startup-config validation tests; stale getPoolConfig mocks
updated to the new schema.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@SWSAmor
SWSAmor merged commit b9f0302 into main Jul 24, 2026
1 of 2 checks passed
@SWSAmor
SWSAmor deleted the fix/mcp-startup-concurrency-retry branch July 24, 2026 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant